//
//  pictureCell.m
//  jiaKaoZhuShou
//
//  Created by 金艺社 on 2018/5/16.
//  Copyright © 2018年 金艺社. All rights reserved.
//

#import "pictureCell.h"

@implementation pictureCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.pictureView_View.userInteractionEnabled = YES;
    if (!_loopView) {
         [self.pictureView_View addSubview:self.loopView];
    }
    self.loopView.tapActionBlock = ^(NSInteger index) {
        NSLog(@"点击了:%ld", (long)index);
    };
}

- (void)setModel:(driveSchoolModel *)model {
    _model = model;
    // 图片数组
    self.pictureAry = model.imageList;
    [self figureShuffling];
    self.name_lab.text = _model.companyName;
    [self.locationBtn setTitle:[NSString stringWithFormat:@"%@%@%@", _model.city, _model.area, _model.areaStore] forState:0];
    [self start:[NSNumber numberWithDouble:_model.start.doubleValue]];
    self.distance_lab.text = _model.distance;
    self.peopleCount_lab.text = [NSString stringWithFormat:@"%@人评价", _model.commentCount];
}
- (void)start:(NSNumber *)number {
    //float f = 1.6; int a; a = floor(f); NSLog("a = %d",a);
    float a = number.floatValue;
    int b = floor(a);//整个星
    float c = a - b;//半个星
    NSArray *subViews = [self.contentView subviews];
    for (UIView *V in subViews) {
        if ([V isKindOfClass:[UIImageView class]] && V.tag == 110) {
            UIImageView *imgView = (UIImageView *)V;
            imgView.image = [UIImage imageNamed:@"greaStars"];
            if (b > 0) {
                imgView.image = [UIImage imageNamed:@"yellowStars"];
                b--;
            }else if (c > 0) {
                imgView.image = [UIImage imageNamed:@"halfStars"];
                c = 0.0;
            }
        }
    }
}
- (void)updateImgWithStr:(NSString *)urlStr {
    NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlStr]];
    UIImage *img = [UIImage imageWithData:data];
    NSMutableArray *images = [[NSMutableArray alloc]initWithObjects:img, nil];
    ZKLoopView *loopView = [[ZKLoopView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, self.pictureView_View.frame.size.height)];
    loopView.images = images;
    [self.pictureView_View addSubview:loopView];
}

- (ZKLoopView *)loopView {
    if (!_loopView) {
        _loopView = [[ZKLoopView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, self.pictureView_View.frame.size.height)];
    }
    return _loopView;
}
- (void)figureShuffling { // 轮播图
    CGFloat H = self.pictureView_View.frame.size.height;
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        // 处理耗时操作的代码块...
        NSMutableArray *images = [NSMutableArray array];
        for (int i = 0; i < self.pictureAry.count; i++) {
            NSString *oss_url = self.pictureAry[i];
            if ([oss_url hasPrefix:@"http"]) {
                oss_url = oss_url.maker.resize.height(@(H)).resultString;
                NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:oss_url]];
                if (data) {
                    UIImage *img = [UIImage imageWithData:data];
                    [images addObject:img];
                }
            }
        }
        //通知主线程刷新
        dispatch_async(dispatch_get_main_queue(), ^{
            self.loopView.images = images;
        });
    });
    typeof(self) weak = self;
    self.loopView.tapActionBlock = ^(NSInteger index) {
        NSLog(@"点击了:%ld", (long)index);
    };
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
@end
